package com.zhengsr.drawbroad.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.zhengsr.drawbroad.ActionType;
import com.zhengsr.drawbroad.state.BaseAction;
import com.zhengsr.drawbroad.state.PenAction;
import com.zhengsr.drawbroad.utils.BgUtils;
import com.zhengsr.drawbroad.utils.CommandUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

/**
 * Created by zhengshaorui
 * time: 2018/7/22
 */

public class DrawView extends SurfaceView implements SurfaceHolder.Callback{
    private static final String TAG = "DrawView";
    private SurfaceHolder mSurfaceHolder;
    private BaseAction mCurrentAction;
    private Bitmap mBitmap;
    private Canvas mCacheCans;
    private Bitmap mBg;
    private List<BaseAction> mBaseState = new ArrayList<>();
    private Executor executor = Executors.newFixedThreadPool(1);
    private ActionType mCurrentType;
    public DrawView(Context context) {
        this(context,null);
    }

    public DrawView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setClickable(true);
        mSurfaceHolder = getHolder();
        mSurfaceHolder.addCallback(this);
        mSurfaceHolder.setFormat(PixelFormat.TRANSLUCENT);
        int width = getResources().getDisplayMetrics().widthPixels;
        int height = getResources().getDisplayMetrics().heightPixels;
        mBitmap = Bitmap.createBitmap(width,height, Bitmap.Config.ARGB_8888);
        mCacheCans = new Canvas(mBitmap);

        mBg = BgUtils.drawGrayLineBg(1920,1080);

        //默认为画笔模式
        mCurrentAction = PenAction.create();
        mCurrentType = mCurrentAction.getViewType();
        mBaseState.add(mCurrentAction);

    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        Canvas canvas = mSurfaceHolder.lockCanvas();
        canvas.drawBitmap(mBg,0,0,null);
        mSurfaceHolder.unlockCanvasAndPost(canvas);

    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {

    }

    public void setCurrentState(BaseAction state){
        mCurrentAction = state;
        mBaseState.add(mCurrentAction);
        mCurrentType = mCurrentAction.getViewType();
    }

    public BaseAction getCurrentState(){
        return mCurrentAction;
    }

   



    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN){
            getActionType();
        }
        mCurrentAction.onTouchEvent(event);
        draw();
        if (event.getAction() == MotionEvent.ACTION_UP){
            mBaseState.add(mCurrentAction);
            mCurrentType = null;
        }
        return true;
    }



    private void getActionType(){
        switch (mCurrentType){
            case PEN:
                mCurrentAction = PenAction.create();
                break;
            default:
                break;
        }
        mCurrentType = mCurrentAction.getViewType();
    }

    private boolean isRedraw;

    private void draw() {

        Canvas canvas = null;
        try {
            canvas = mSurfaceHolder.lockCanvas();
            mCacheCans.drawBitmap(mBg,0,0,null);
            for (BaseAction state : mBaseState) {
                state.draw(mCacheCans);
            }
            canvas.drawBitmap(mBitmap,0,0,null);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (canvas != null) {
                mSurfaceHolder.unlockCanvasAndPost(canvas);
            }
        }


    }


    /**
     * 清屏函数
     */
    public void clear(){
        Canvas canvas = mSurfaceHolder.lockCanvas();
        canvas.drawBitmap(mBg,0,0,null);
        mSurfaceHolder.unlockCanvasAndPost(canvas);
        CommandUtils.getInstance().clear();

        mBaseState.clear();


    }

    public void setPaintColor(int color){
        if (mCurrentAction instanceof PenAction){
            PenAction state = (PenAction) mCurrentAction;
            state.setColor(color);
        }
    }

    public void undo(){
        CommandUtils.getInstance().undo();
        isRedraw = true;

    }
}
